import { isArray } from 'lodash-es';

export const flattenData = function flattenData(data) {
    const flattenedArray = [];

    data.forEach(project => {
        const { projectCode, projectName } = project; //事业部/项目

        project.statisticsList.forEach((personStats, projectIndex) => {
            const {
                chargePersonId,
                chargePerson, //  项目/人
                statisticsInfo: { statisticsList = [], ...lastSecond },
            } = personStats;

            if ((statisticsList || []).length > 0) {
                (statisticsList || []).map((cate, orderIndex) => {
                    //公区/户内的分类
                    if ((cate.statisticsList || []).length > 0) {
                        cate.statisticsList.map((secondCate, cateIndex) => {
                            //子集分类数据
                            const params: any = {
                                ...lastSecond,
                                ...secondCate,
                                orderType: cate.orderType,
                                classifyName: secondCate.classifyName,
                                chargePersonId,
                                chargePerson,
                                projectCode,
                                projectName,
                                cateIndex,
                                cateLength: (cate.statisticsList || []).length || 0,
                                orderIndex,
                                orderLength: (statisticsList || []).length || 0,
                                projectIndex: projectIndex + orderIndex + cateIndex,
                                chargePersonIndex: orderIndex + cateIndex,
                                projectLength: isArray(statisticsList)
                                    ? statisticsList.reduce((pi, ci) => {
                                          return pi + (ci.statisticsList || []).length;
                                      }, 0)
                                    : 1,
                                totalLength: project.statisticsList.reduce((pre, cur) => {
                                    return (
                                        pre +
                                        (isArray(cur.statisticsInfo.statisticsList)
                                            ? (cur.statisticsInfo.statisticsList || []).reduce((pi, ci) => {
                                                  return pi + (ci.statisticsList || []).length;
                                              }, 0)
                                            : 1)
                                    );
                                }, 0),
                            };
                            flattenedArray.push(params);
                        });
                    } else {
                        flattenedArray.push({
                            chargePersonId,
                            chargePerson,
                            projectCode,
                            projectName,
                            cateIndex: 0,
                            cateLength: (cate.statisticsList || []).length || 0,
                            orderIndex,
                            orderLength: (statisticsList || []).length || 0,
                            projectIndex,
                            // projectLength: (project.statisticsList || []).length,
                            totalLength: 1,
                            projectLength: 1,
                            chargePersonIndex: 1,
                        });
                    }
                });
            } else {
                flattenedArray.push({
                    ...(lastSecond || {}),
                    chargePersonId,
                    chargePerson,
                    projectCode,
                    projectName,
                    cateIndex: 0,
                    cateLength: 0,
                    orderIndex: 0,
                    orderLength: 0,
                    projectIndex,
                    // projectLength: (project.statisticsList || []).length,
                    totalLength: 1,
                    projectLength: 1,
                    chargePersonIndex: 1,
                });
            }
        });
    });

    return flattenedArray;
};
